// pages/welcome/welcome.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var m_video = wx.createVideoContext('myVideo', this);
Page({

  /**
   * 页面的初始数据
   */
  data: {

    checkedHot: true,
    checkedOther: false,
  },
  //banner点击跳转
  onItemBanner(e) {
    var link = e.currentTarget.dataset.link;
    if (!util.isEmpy(link)) {
      if (link.indexOf("http") >= 0) {
        util.toClass('/pages/myweb/myweb', {
          url: link
        })
      } else {
        wx.navigateTo({
          url: link,
        })
      }

    }
  },

  //点击标题
  onItemTilteClick(e) {
    var tid = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var titleList = this.data.titleList;

    for (var x in titleList) {
      titleList[x].deleted = false;
    }
    if (titleList[index].id == tid) {
      titleList[index].deleted = true;
    }

    this.setData({
      titleList: titleList,
      checkedHot: false,
      checkedOther: true
    })
    this.getListData(tid, 0)
  },

  //跳转充值中心
  onRechargeCenter(e) {
    wx.navigateTo({
      url: '/pages/recharge-center/recharge-center',
    })
  },
  //跳转电子卡
  onCardCenter(e) {
    wx.navigateTo({
      url: '/pages/card/card',
    })
  },
  //跳转到搜索
  onSearch(e) {
    wx.navigateTo({
      url: '/pages/search-index/seach-index',
    })
  },
  //扫一扫
  onScan(e) {
    var that = this;
    wx.scanCode({
      success(res) {
        console.log("二维码", res)
        var result = res.result;
        var goodsid = '';
        if (util.isEmpy(result)) {
          util.myToast("二维码错误")
          return
        } else {
          if (result.indexOf(',') > 0) {
            //二维码
            var arrPara = result.split(",");
            var arrUserid = arrPara[0].split(":");
            var arrGoodsid = arrPara[1].split(":");
            goodsid = arrGoodsid[1];
            wx.setStorageSync("shareUserId", arrUserid[1])
            util.toClass('/pages/details/details', {
              goodsid: goodsid
            })
          } else {
            //条形码
            that.getQueGoodsIdData(result);
          }
        }


      },
    })
  },
  //根据商品编号查询商品id
  getQueGoodsIdData(goodsSn) {
    let that = this;
    util.request(api.QueGoodsId, {
      goodsSn: goodsSn
    }).then(function (res) {
      if (res.errno == 0) {
        util.toClass('/pages/details/details', {
          goodsid: res.data.id
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //跳转会员中心
  onVipCenter(e) {
    wx.navigateTo({
      url: '/pages/vip_index/vip_index',
    })
  },
  //获取数据
  getData() {
    let that = this;
    util.request(api.Index).then(function (res) {
      if (res.errno == 0) {
        var titleList = res.data.categorys;

        //默认选中第一个
        titleList[0].deleted = true;

        that.setData({
          bannerList: res.data.banners,
          newsGoodsList: res.data.newsGoods,
          titleList: titleList,
          videos: res.data.videos,
          hotGoodsArr:res.data.hotsGoods
        })
        that.getListData(titleList[0].id,0);
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //获取分类数据
  getListData(categoryId, isHot) {
    let that = this;
    util.request(api.List, {
      categoryId: categoryId,
      isHot: isHot,
      goodsType: 1
    }).then(function (res) {
      if (res.errno == 0) {
        that.setData({
          hotsGoodsList: res.data
        })

      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //点击进入详情购买
  onItemClick(e) {
    var goodsid = e.currentTarget.dataset.id;
    util.toClass('/pages/details/details', {
      goodsid: goodsid
    })
  },


  // 跳转领券中心
  onReceive() {
    wx.navigateTo({
      url: '../receive/receive',
    })
  },

  // 跳转邀请分享
  onShare() {
    wx.navigateTo({
      url: '../share/share',
    })
  },

  //  跳转文章
  onFine() {
    wx.navigateTo({
      url: '../find_index/find_index',
    })
  },



  //新人专区查看更多
  onLookMore(e) {
    wx.navigateTo({
      url: '/pages/new_list/new_list',
    })
  },
  //进入游戏
  onPlayGame(e) {
    wx.navigateTo({
      url: '/pages/game/game_index/game_index',
    })
    // util.myToast("正在研发")
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.stopPullDownRefresh({
      success: (res) => {},
    })
    this.getData();
    
    //获取通过分享进入小程序携带的参数
    var uid = options.userid;
    const scene = decodeURIComponent(options.scene)
    if (uid == undefined) {
      uid = ""
    }
    wx.setStorageSync("shareUserId", uid)
    if (scene != undefined && scene != "undefined") {

      var arrUserid = scene.split(":");
      //存入上级代理的UserID
      wx.setStorageSync("shareUserId", arrUserid[1])

    };

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    m_video.pause();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    m_video.stop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getData();
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: "傣王稻勐海香米，一键领取优惠",
      path: '/pages/index/index?userid=' + wx.getStorageSync("userid"),
      imageUrl: 'https://zebra-1258707023.cos.ap-chengdu.myqcloud.com/yndwd/share-img.jpg'
    }
  },

})